﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Pibby.Core.Tests.Stubs
{
	public class M3uPlaylistGenerator : IGenerateContent
	{
		private StringBuilder _builder = new StringBuilder();

		#region Constructors

		public M3uPlaylistGenerator()
		{
		}

		#endregion Constructors

		#region Properties

		public int HowManyItems
		{
			get;
			set;
		}

		#endregion Properties

		#region Generation

		private void CreatePlaylist()
		{
			BuildHeader();

			for (int i = 0; i < this.HowManyItems; ++i)
			{
				BuildItem(string.Format("..\\My Music\\Music_{0}.mp3", i + 1), string.Format("Music_{0}.mp3", i + 1));
			}

			BuildFooter();
		}

		private void BuildHeader()
		{
			this._builder.AppendLine ("#EXTM3U");
		}

		private void BuildItem(string relativePath, string fileName)
		{
			this._builder.AppendFormat("#EXTINF:0,{0}\r\n", fileName);
			this._builder.AppendFormat("{0}\r\n\r\n", relativePath);
		}

		private void BuildFooter()
		{
		}

		#endregion Generation


		#region IGenerateContent

		public string Content
		{
			get
			{
				this.CreatePlaylist();

				return this._builder.ToString();
			}
		}

		public TextReader ContentStream
		{
			get
			{
				return new StringReader(this.Content);
			}
		}


		#endregion
	}
}
